
/*
 * Funcoes de Formulario
 */

function validacampos(formulario) {

	for (i = 0; i < formulario.childNodes.length; i++) {
		if (formulario.childNodes[i].tagName == "INPUT" && (formulario.childNodes[i].type == "text"
			|| formulario.childNodes[i].type == "password")) {
			if (formulario.childNodes[i].value == "") {
				formulario.childNodes[i].focus();
				formulario.childNodes[i].style.background = "#ffffff";
				formulario.childNodes[i].style.borderColor = "#cbd7c4";
				return false;
			} 
		}
	}
	return true;
}
function getParametrosDoFormulario(formulario) {
	
	var str = "";
	
	for (i = 0; i < formulario.childNodes.length; i++) {
		
		
		if (formulario.childNodes[i].tagName == "INPUT") {
			
			if (formulario.childNodes[i].type == "text" || formulario.childNodes[i].type == "hidden" || formulario.childNodes[i].type == "password") {
				
				str += formulario.childNodes[i].name + "=" + formulario.childNodes[i].value;
				str +="&";
			}
			
		}
		
		if (formulario.childNodes[i].tagName == "TABLE") {
			var tbl = formulario.childNodes[i];
			
			for(m=0;m<tbl.rows.length-1;m++){
             
                for(j=0;j<tbl.rows[m].childNodes.length;j++){
                	
                	for(k=0;k<tbl.rows[m].childNodes[j].childNodes.length;k++){
                		
                		if(tbl.rows[m].childNodes[j].childNodes[k].type=="checkbox"){
                            
                            if(tbl.rows[m].childNodes[j].childNodes[k].checked){
                            	str += tbl.rows[m].childNodes[j].childNodes[k].name + "=";
                            	str += tbl.rows[m].childNodes[j].childNodes[k].value;
                            	str +="&";
                            }
                		}
                	}
                }

			}

		}
	}	
	return str;
}
function enviaFormAjax(acao,formulario,elemento_result) {
	
	if(!validacampos(formulario)){
		return false;
	}
	
	
	var params = getParametrosDoFormulario(formulario);
	
	enviaConsulta(acao,params,elemento_result);
}

function getTamanhoCampoFormulario(formulario,campo) {
	var str = "";
	
	for (i = 0; i < formulario.childNodes.length; i++) {
		if (formulario.childNodes[i].tagName == "INPUT") {
			if (formulario.childNodes[i].name == campo) {
				str = formulario.childNodes[i].value;
			}
		}
	}
	return str.length;
}
function enviaFormSubmit(formulario,elemento_result) {

	if(!validacampos(formulario)){
		document.getElementById(elemento_result).innerHTML = "Campos em Branco!";
		document.getElementById(elemento_result).style.color = "#cbcbcb";
		return false;
	}else if(getTamanhoCampoFormulario(formulario,"inscricao")!=4){
		document.getElementById(elemento_result).innerHTML = "A inscrição possui 4 digitos!";
		formulario.inscricao.focus();
		formulario.inscricao.style.background = "#ffffff";
		formulario.inscricao.style.borderColor = "#cbd7c4";
		return false;
	}else{
		document.getElementById(elemento_result).innerHTML = "";
	}
	formulario.submit();
	document.getElementById("btn_1cr").disabled = true;
	document.getElementById(elemento_result).innerHTML = "Fazendo download ...";
}

function enviaForm(formulario,elemento_result) {
	if(!validacampos(formulario)){
		document.getElementById(elemento_result).innerHTML = "Campos em Branco!";
		document.getElementById(elemento_result).style.color = "#cbcbcb";
		return false;
	}else{
		document.getElementById(elemento_result).innerHTML = "";
	}
	formulario.submit();
	document.getElementById("btn_1cr").disabled = true;
	document.getElementById(elemento_result).innerHTML = "Fazendo download do Certificado...";
}
/*
 * Funcoes de Validacao
 */
function IsNumber(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (charCode >= 48 && charCode <= 57 || charCode < 20) {
		return true;
	} else {
		return false;
	}
}
function valida_email(field) {
	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos < 1 || dotpos - apos < 2) {
			return false;
		} else {
			return true;
		}
	}
}
function validarCPF(cpf){
	   var cpf = document.getElementById(cpf).value;
	    
	   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		  cpf == "88888888888" || cpf == "99999999999"){
		  window.alert("CPF inválido. Tente novamente.");
		  return false;
	   }

	   soma = 0;
	   for(i = 0; i < 9; i++)
	   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
	   resto = 11 - (soma % 11);
	   if(resto == 10 || resto == 11)
		 resto = 0;
	   if(resto != parseInt(cpf.charAt(9))){
		 window.alert("CPF inválido. Tente novamente.");
		 return false;
	   }
	   soma = 0;
	   for(i = 0; i < 10; i ++)
		 soma += parseInt(cpf.charAt(i)) * (11 - i);
	   resto = 11 - (soma % 11);
	   if(resto == 10 || resto == 11)
		 resto = 0;
	   if(resto != parseInt(cpf.charAt(10))){
	     window.alert("CPF inválido. Tente novamente.");
		 return false;
	   }
	   return true;
}
